{% extends "_base.html" %}
{% load bootstrap3 %}
{% load staticfiles %}

{% block  title %}容器管理{% endblock %}

{% block left %}
    {% include "_left.html" with cmdb_docker_container_active="active" %}
{% endblock left %}


{% block page-content %}


    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>资产管理</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="/">主页</a>
                </li>
                <li>
                    <a href="{% url 'cmdb:docker_host_list' %}">Docker管理</a>
                </li>
                <li class="active">
                    <strong>容器管理 - {{ client.dockerhost }}</strong>{% if netype %} - 网络类型({{ netype }}){% endif %}
                </li>
            </ol>
        </div>
        <div class="col-lg-2">

        </div>
    </div>


    <div class="row wrapper wrapper-content animated fadeInRight">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="col-md-8 form-inline">
                        <label class="text-success">宿主机:</label>
                        
                        <select id="dockerhost" class="form-control">
                            {% if not client %}<option value="">------</option>{% endif %}
                            {% for row in dockerhosts %}
                            <option value="{{ row.id }}" {% if row == client.dockerhost %}selected{% endif %}>{{ row }}</option>
                            {% endfor %}
                        </select>

                    </div>


                </div>
                <div class="ibox-content">
                    {% if error %}
                    {{ error }}
                    {% elif client %}
                    <a href="{% url 'cmdb:docker_container_add' client.dockerhost.id %}" class="btn btn-primary"> 添加</a>
                    <a class="btn btn-danger" id="delall">批量删除</a>

                    <table class="table table-striped table-bordered table-hover dataTables-example" id="tb">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="CheckedAll"></th>
                                <th class="col-xs-4">名称</th>
                                <th class="col-xs-2">镜像</th>
                                <th class="col-xs-1">容器IP</th>
                                <th class="col-xs-1">状态</th>
                                <th class="col-lg-1">创建</th>
                                <th class="col-xs-3">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for i in cons %}
                            <tr>
                                <td><input type="checkbox" value="{{ i.id }}"  name="id"></td>
                                <td>{{ i.name }}</td>
                                <td>{{ i.image }}</td>
                                <td>{{ i.ip }}</td>
                                <td>{{ i.status }}</td>
                                <td>{{ i.time }}</td>
                                {# <td>{{ i.runtime }}</td> #}

                                <td>
                                    <button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal">详情</button>
                                    <button type="button" class="btn btn-success btn-xs" data-toggle="modal" data-target="#myModal">日志</button>
                                    <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal">启动</button>
                                    <button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myModal">停止</button>
                                    <a class="btn btn-success btn-xs btn-outline" href="{% url 'cmdb:docker_webssh' %}?host={{ client.dockerhost.id }}&container={{ i.name }}" target="_blank">终端</a>
                                    <button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModal">删除</button>
                                </td>
                            </tr>
                            {% endfor %}


                        </tbody>

                    </table>
                    {% else %}
                    请选择宿主机/物理机
                    {% endif %}

                </div>
            </div>
        </div>


    </div>


    <!-- 操作返回 - 模态框（Modal） -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="false">
                        &times;
                    </button>
                    <span id="server"></span>
                </div>
                <div class="modal-body">
                    <textarea id="server_msg" readonly style="width:100%;" rows="5">{{ text }}</textarea>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                </div>
            </div>
        </div>
    </div>


{% endblock %}


{% block footer-js %}
    <script>
        function conn_docker(url, textarea_height){
            $.ajax({
                url: url,
                type: 'GET',
                timeout: 20000,
                error: function(){
                    $("#server_msg").html('<option value="">网站连接失败！请联系CMDB管理员</option>');
                },
                success: function(msg_html){
                    // json格式化
                    // console.log(msg_html);
                    try{
                        var msg = JSON.parse(msg_html);
                        console.log(msg.remove);
                        if (msg.remove=="ok") {
                            // 删除成功，刷新页面
                            location.reload()
                        }
                        var msg_html = JSON.stringify(msg, null, 4);
                    }catch(err){}finally{
                        $('#server_msg').val(msg_html);
                        // console.log(txt);
                        $('#server_msg')[0].style.minHeight= textarea_height;
                    } 
                }
            });
        };

        $(document).ready(function(){

            $('#dockerhost').change(function() {
                // 宿主机下拉框选择后
                if (this.value) {
                    console.log(this.value);
                    window.location.href="{% url 'cmdb:docker_container' %}"+this.value+"/"
                }
            });

            {% if client %}

            // $( ".btn-danger.btn-xs" ).click()
            $('#tb').on('click', 'button', function (e) {
                // console.log($(this));
                var url="{% url 'cmdb:docker_container_do' client.dockerhost.id %}?id="+this.parentElement.parentElement.children[0].children[0].value;
                var textarea_height="400px";
                if (this.className=="btn btn-danger btn-xs") {
                    if(! window.confirm('确认删除容器？')){
                        return false;
                    }
                    var url="{% url 'cmdb:docker_container_rm' client.dockerhost.id %}?id="+this.parentElement.parentElement.children[0].children[0].value;
                    textarea_height="100px";
                } else if (this.className=="btn btn-primary btn-xs") {
                    url=url+"&do=start";
                    textarea_height="100px";
                } else if (this.className=="btn btn-success btn-xs btn-outline") {
                    // 终端、命令
                    url=url+"&do=exec_run";
                    // return false;
                } else if (this.className=="btn btn-success btn-xs") {
                    url=url+"&do=logs";
                } else if (this.className=="btn btn-info btn-xs") {
                    url=url+"&do=attrs";
                } else if (this.className=="btn btn-warning btn-xs") {
                    url=url+"&do=stop";
                    textarea_height="100px";
                }

                $("#server_div").show();
                $("#server").text(this.parentElement.parentElement.children[1].textContent+" "+this.textContent);
                // console.log(url);
                conn_docker(url, textarea_height);

            });


            $("#delall").click(function(){
                var container_ids = '';
                var cs=document.getElementsByName('id');
                for (i=0; i < cs.length; i++) {
                    if (cs[i].checked) {
                        container_ids = container_ids+cs[i].value+',';
                        // console.log(cs[i].value);
                    }
                }
                if (! container_ids) {return false;}
                if(! window.confirm('确认批量删除容器？')){
                    return false;
                }
                var url="{% url 'cmdb:docker_container_rm' client.dockerhost.id %}?id="+container_ids;
                console.log(container_ids);
                $("#server_div").show();
                $("#server").text("批量删除容器");
                conn_docker(url, "100px");
            });
            {% endif %}

        });


    </script>
{% endblock footer-js %}


